package com.qf.controller;

import com.qf.util.CookieUtil;
import com.qf.util.JwtUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.UUID;

/**
 * @author 千锋教育
 * @Company http://www.mobiletrain.org/
 * @Version 1.0
 */
@RestController
@RequestMapping("/auth")
public class LoginController {

    @Value("${custom.password}")
    private String password;

    @Value("${custom.username}")
    private String username;


    @RequestMapping("/login")
    public String login(String username , String password, HttpServletResponse response) {
        if(!StringUtils.isEmpty(username) && !StringUtils.isEmpty(password)) {
            //如果账号和密码匹配，则登录成功
            if(username.equalsIgnoreCase(this.username) && password.equalsIgnoreCase(this.password)) {
                System.out.println("登录成功");

                String jwt = JwtUtil.createJWT(UUID.randomUUID().toString(), username, null);

                CookieUtil.addCookie(response, "localhost", "/", "jwtToken", jwt, -1, false);

                return "login success!!";
            }
        }

        return "login error!!";
    }
}
